package com.net.tool;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.tools.DailyYogaTools;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class ServerRootURLConfigure {

	static final boolean DEBUG = true;

	Context mContext;

	private static ServerRootURLConfigure mServerRootURLConfigure;

	public static ServerRootURLConfigure getServerRootURLConfigure(
			Context context) {

		if (mServerRootURLConfigure == null) {
			mServerRootURLConfigure = new ServerRootURLConfigure();
		}

		mServerRootURLConfigure.mContext = context;

		return mServerRootURLConfigure;

	}

	/**
	 * 获取课程相关的总连接
	 * 
	 * @return
	 */
	public String getSessionRootURL() {

		try {
			return new JSONObject(getConfigureContent())
					.getString("SessionRootURL");
		} catch (Exception e) {
			return "http://androiddailyyogacn.oss.aliyuncs.com/";
		}

	}

	/**
	 * 获取插件数据表的总连接
	 * 
	 * @return
	 */
	public String[] getPlugsRootURLs() {

		try {

			JSONArray array = new JSONObject(getConfigureContent())
					.getJSONArray("PlugsRootURLs");

			String[] strings = new String[array.length()];

			for (int i = 0; i < strings.length; i++) {
				strings[i] = array.getString(i);
			}

			return strings;

		} catch (Exception e) {
			return new String[] { "http://androiddailyyogacn.oss.aliyuncs.com/sesssion_plugurls/session_urls.xml" };
		}

	}

	public String getRecommendRootURL() {
		try {

			return new JSONObject(getConfigureContent())
					.getString("RecommendRootURL");

		} catch (Exception e) {

			return "http://androiddailyyogacn.oss.aliyuncs.com/recommend/";

		}
	}

	/**
	 * 获取社区API的根URL
	 * 
	 * @return
	 */

	public String getCommunityRootURl() {

		try {
			if (DEBUG) {
				return "http://192.168.200.145/members/test140624/"; // 测试地址
			} else {
				return new JSONObject(getConfigureContent())
						.getString("CommunityRootURl") + "cncommunity131106/";
			}
		} catch (Exception e) {
			e.printStackTrace();

			if (DEBUG) {
				return "http://115.29.187.128/members/cncommunity131106/";
			} else {
				return "http://115.29.187.128/members/cncommunity131106/";
			}
		}

	}

	private int getConfigVc() {
		try {
			return new JSONObject(getConfigureContent()).getInt("ConfigVc");
		} catch (Exception e) {
			return -1;
		}

	}

	private String getConfigureContent() {

		synchronized (getClass()) {
			SharedPreferences sharedPreferences = mContext
					.getSharedPreferences("ServerRootURLConfigure", 0);

			return sharedPreferences.getString("content", null);
		}

	}

	private void setConfigureContent(String content) {
		synchronized (getClass()) {
			SharedPreferences sharedPreferences = mContext
					.getSharedPreferences("ServerRootURLConfigure", 0);
			sharedPreferences.edit().putString("content", content).commit();
		}
	}

	HttpURLConnection mHttpURLConnection;

	/**
	 * 升级服务器配置
	 * 
	 * @return
	 */
	public boolean updateSeverConfigure() {

		Log.d("download", "updateSeverConfigure");

		int vc = getConfigVc();
		try {
			URL url = new URL(
					"http://androiddailyyogacn.oss.aliyuncs.com/config_server/ServerRootURLConfigure.json");
			mHttpURLConnection = (HttpURLConnection) url.openConnection();
			byte[] content = new byte[mHttpURLConnection.getContentLength()];
			InputStream inputStream = mHttpURLConnection.getInputStream();
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					inputStream);
			bufferedInputStream.read(content);
			bufferedInputStream.close();
			inputStream.close();

			Log.d("download", "updateSeverConfigure="
					+ new String(content, "utf-8"));

			setConfigureContent(new String(content, "utf-8"));

		} catch (Exception e) {

			e.printStackTrace();

		} finally {

			if (mHttpURLConnection != null) {
				mHttpURLConnection.disconnect();
				mHttpURLConnection = null;
			}

		}
		return vc > 0 && vc < getConfigVc();
	}

	public void cancalUpdateServerConfigure() {

		if (mHttpURLConnection != null) {
			mHttpURLConnection.disconnect();
			mHttpURLConnection = null;
		}

	}

	public String getLauncherURl() {

		try {
			return new JSONObject(getConfigureContent())
					.getString("LauncherURl");
		} catch (Exception e) {
			return "http://androiddailyyogacn.oss.aliyuncs.com/launcher/";
		}
	}

	public boolean isForumOpen() {

		try {

			return new JSONObject(getConfigureContent()).getBoolean("Forum");

		} catch (Exception e) {

			return true;
		}
	}

	public boolean isDisplayPublish() {
		// try {
		// return new JSONObject(getConfigureContent())
		// .getBoolean("DisplayPublish");
		// } catch (Exception e) {
		// e.printStackTrace();
		//
		// return true;
		// }

		return false;

	}

	public boolean isDisplayAd() {
		try {
			return new JSONObject(getConfigureContent())
					.getBoolean("DisplayAD");
		} catch (Exception e) {
			return false;
		}
	}

	public boolean plugsMarketSwitch() {

		try {
			return new JSONObject(getConfigureContent()).getJSONObject(
					"PlugsMarket").getBoolean("state");
		} catch (Exception e) {
			return false;
		}

	}

	public int getSessionIndex() {

		try {
			return new JSONObject(getConfigureContent()).getInt("SessionIndex");
		} catch (Exception e) {
			return 0;
		}

	}

	public List<String> getPlugsMarket() {

		try {
			List<String> plugs = new ArrayList<String>();
			JSONArray array = new JSONObject(getConfigureContent())
					.getJSONObject("PlugsMarket").getJSONArray("market");
			for (int i = 0; i < array.length(); i++) {
				if (DailyYogaTools.pkgIsInstall(array.getString(i), mContext)) {
					plugs.add(array.getString(i));
					break;
				}
			}
			return plugs;

		} catch (Exception e) {
			return null;
		}

	}

	public int getRemindT() {
		try {
			return new JSONObject(getConfigureContent()).getInt("RemindT");
		} catch (Exception e) {

			return 3;
		}
	}

	public boolean isSinUp() {
		try {
			return new JSONObject(getConfigureContent()).getBoolean("SinUp");
		} catch (Exception e) {
			return false;
		}
	}

	public boolean isFormManeger(String uId) {

		try {
			JSONArray array = new JSONObject(getConfigureContent())
					.getJSONArray("ForumManager");
			
Log.d("kkk0", "--"+getConfigureContent()+"");
			for (int i = 0; i < array.length(); i++) {
				if (array.getString(i).equals(uId)) {
					return true;
				}
			}

		} catch (Exception e) {
Log.d("kkk1", "--"+getConfigureContent()+"");
			e.printStackTrace();
		}

		return false;

	}

	public String getLangURL() {
		// TODO Auto-generated method stub
		return "";
	}

	public int getSessionVc() {
		try {
			return new JSONObject(getConfigureContent()).getInt("SessionVc");
		} catch (Exception e) {
			return 1;
		}
	}

	public int getProgramVc() {
		try {
			return new JSONObject(getConfigureContent()).getInt("ProgramVc");
		} catch (Exception e) {
			return 1;
		}
	}

	public int getPuechaseVc() {
		try {
			return new JSONObject(getConfigureContent()).getInt("PurchaseVc");
		} catch (Exception e) {
			return 1;
		}
	}

	
	
	public String getPurchaseURL() {
		return "http://115.29.187.128/paytest/";
	}

}
